<?php
namespace control;
/*
*腾讯云API类
*/

class tencentApiClass extends adIndexClass
{
    private const title = '腾讯云API类';
    private string $secretId;//API ID
    private string $secretKey;//API 秘钥
    private int $timestamp;//时间戳
    private string $httpRequestMethod = 'POST';//请求方式
    private string $algorithm='TC3-HMAC-SHA256';//签名方法
    private string $canonicalUri = "/";
    private string $canonicalQueryString = "";
    private string $canonicalHeaders;
    private string $signedHeaders = "content-type;host";
    private string $service;
    private function initPara()
    {
        $this->secretId = para('tencentApiSecretId');
        $this->secretKey = para('tencentApiSecretKey');
        $this->timestamp = time();
    }

    /**
     * 发票核验API
     */
    public function vatInvoiceVerify($InvoiceCode, $InvoiceNo, $InvoiceDate, $Additional): bool|string
    {
        $this->initPara();
        $host = "ocr.tencentcloudapi.com";
        $domin ='https://'.$host;
        $this->service = "ocr";//这个参数就是host的前袋
        $version = "2018-11-19";
        $action = "VatInvoiceVerify";//.方法
        $region = "ap-guangzhou";//地域
        $this->canonicalHeaders = "content-type:application/json\n" . "host:" . $host . "\n";
        //参数
        $params = array(
            'InvoiceCode' => $InvoiceCode,
            'InvoiceNo' => $InvoiceNo,
            'InvoiceDate' => $InvoiceDate,
            'Additional' => $Additional,
        );
        $params = json_encode($params);
        $authorization = $this->signature($params);

        $herader = array();
        array_push($herader, "Authorization:" . $authorization);
        array_push($herader, "Content-Type: application/json");
        array_push($herader, "Host:" . $host);
        array_push($herader, "X-TC-Region:" . $region);
        array_push($herader, "X-TC-Action:" . $action);
        array_push($herader, "X-TC-Version:" . $version);
        array_push($herader, "X-TC-Timestamp:" . $this->timestamp);
        array_push($herader, "X-TC-Language:zh-CN");
        return self::curlPost($domin, $params, $herader, 60, false);
    }

    /**
     * 发票识别OCR
     */
    public function vatInvoiceOCR($ImageUrl): bool|string
    {
        $this->initPara();
        $host = "ocr.tencentcloudapi.com";
        $domin ='https://'.$host;
        $this->service = "ocr";//这个参数就是host的前袋
        $version = "2018-11-19";
        $action = "VatInvoiceOCR";//方法
        $region = "ap-guangzhou";//地域
        $this->canonicalHeaders = "content-type:application/json\n" . "host:" . $host . "\n";
        //参数
        $params = array(
            'ImageUrl' => $ImageUrl,
            'IsPdf' => true,
        );
        $params = json_encode($params);
        $authorization = $this->signature($params);

        $herader = array();
        array_push($herader, "Authorization:" . $authorization);
        array_push($herader, "Content-Type: application/json");
        array_push($herader, "Host:" . $host);
        array_push($herader, "X-TC-Region:" . $region);
        array_push($herader, "X-TC-Action:" . $action);
        array_push($herader, "X-TC-Version:" . $version);
        array_push($herader, "X-TC-Timestamp:" . $this->timestamp);
        array_push($herader, "X-TC-Language:zh-CN");
        return self::curlPost($domin, $params, $herader, 60, false);
    }

    /**
     * 生成签名
     * @param string $params 参数 json
     * @return string
     */
    private function signature(string $params): string
    {
        $hashedRequestPayload = hash('SHA256', $params);
        $canonicalRequest = $this->httpRequestMethod . "\n" . $this->canonicalUri . "\n" . $this->canonicalQueryString . "\n" . $this->canonicalHeaders . "\n" . $this->signedHeaders . "\n" . $hashedRequestPayload;

        //step2
        $date = gmdate("Y-m-d", $this->timestamp);
        $credentialScope = $date . "/" . $this->service . "/tc3_request";
        $hashedCanonicalRequest = hash("SHA256", $canonicalRequest);
        $stringToSign = $this->algorithm . "\n" . $this->timestamp . "\n" . $credentialScope . "\n" . $hashedCanonicalRequest;

        //step3
        $secretDate = hash_hmac("SHA256", $date, "TC3" . $this->secretKey, true);
        $secretService = hash_hmac("SHA256", $this->service, $secretDate, true);
        $secretSigning = hash_hmac("SHA256", "tc3_request", $secretService, true);
        $signature = hash_hmac("SHA256", $stringToSign, $secretSigning);
        return $this->algorithm . "Credential=" . $this->secretId . "/" . $credentialScope . ",SignedHeaders=content-type;host,Signature=" . $signature;
    }
}

